<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 *
 * @abstract 产品管理
 * @author zxj
 *
 */
class Product_new extends MY_Controller
{
    private $t_product = 'mt_product';  //产品表

    function __construct()
    {
        parent::__construct();
        $this->load->model('fee_model', 'feeModel');
    }


    /**
     * 更新状态
     * @param string $id
     */
    function setStatus($id = '')
    {
        $id = intval($id);
        if (!$id) show_error('not found this is product');
        $this->common_model->resetStatus($id);
    }


    /**
     * 查看详情
     * @param string $id
     * @return mixture
     */
    function inDetails($id = '')
    {
        $id = empty($id) ? gp('id') : $id;
        //产品信息
        $productFirst = $this->common_model->selectFirst($this->t_product, array('id' => $id));
        $status_id = $productFirst['status_id'];
        if ($productFirst['brandCustomerNum']) {
            $brandCustomer = $this->common_model->selectFirst('mt_customers', array('id' => $productFirst['brandCustomerNum']));
            $data['brandCustomer'] = $brandCustomer;
        }
        //包裹信息
        if ($status_id == 14 || $status_id == 17) {
            $packages = $this->common_model->selectFirst('mt_package', array('product_id' => $productFirst['id']));
            $data['packages'] = $packages;
        }
        $categoryFirst = $this->common_model->selectFirst('mt_product_category', array('id' => $productFirst['category_id']));
        $statusFirst = $this->common_model->selectFirst('mt_product_status', array('id' => $productFirst['status_id']));
        $productFirst['category_name'] = $categoryFirst['name_' . $this->curLang];
        $productFirst['status_name'] = $statusFirst['name_' . $this->curLang];
        $productFirst['add_time'] = date('Y-m-d H:i:s', $productFirst['add_time']);
        if (isset($productFirst['isUnpackPass'])) {
            $productFirst['isUnpackPass'] = $productFirst['isUnpackPass'] ? 'Yes' : 'No';
        }
        if (isset($productFirst['isQcPass'])) {
            $productFirst['isQcPass'] = $productFirst['isQcPass'] ? 'Yes' : 'No';
        }
        if (isset($productFirst['acceptQuotation'])) {
            $productFirst['acceptQuotation'] = $productFirst['acceptQuotation'] ? 'Yes' : 'No';
        }
        if (isset($productFirst['supplementAcceptQuotation'])) {
            $productFirst['supplementAcceptQuotation'] = $productFirst['supplementAcceptQuotation'] ? 'Yes' : 'No';
        }
        if (isset($productFirst['needToConfirm'])) {
            $productFirst['needToConfirm'] = $productFirst['needToConfirm'] ? 'Yes' : 'No';
        }
        if (isset($productFirst['inWarranty'])) {
            $productFirst['inWarrantyName'] = $productFirst['inWarranty'] ? 'Yes' : 'No';
        }
        if (isset($productFirst['inWarranty_qc'])) {
            $productFirst['inWarranty_qcName'] = $productFirst['inWarranty_qc'] ? 'Yes' : 'No';
        }
        if (isset($productFirst['inWarranty_tr'])) {
            if ($productFirst['inWarranty_tr'] == 1) {
                $productFirst['inWarranty_trName'] = 'This device has manufacturer warranty.';
            } else {
                $productFirst['inWarranty_trName'] = 'We do not find manufacturer warranty.';
            }
        }
        if (isset($productFirst['seller_warranty'])) {
            $productFirst['seller_warrantyName'] = $productFirst['seller_warranty'] ? 'Yes' : 'No';
        }
        //获取订单信息
        $orderFirst = $this->common_model->selectFirst('mt_order', array('id' => $productFirst['oid']));
        $isPay = $orderFirst['is_pay'];
        $data['orderFirst'] = $orderFirst;
        //产品成色
        if ($productFirst['physicalAppearance']) {
            $damageFirst = $this->common_model->selectFirst('mt_product_damage', array('id' => $productFirst['physicalAppearance']));
            $productFirst['damageName'] = $damageFirst['name_' . $this->curLang];
        }
        //#随机附件
        if ($productFirst['accessories']) {
            $accessoriesList = $this->common_model->selectCustom('mt_product_accessory', '', 'id desc', array('id' => explode(',', $productFirst['accessories'])));

            //自定义随机配件 begin
            $acc_cuz = $this->common_model->selectCustom('mt_product_accessory_customize', array('category' => $productFirst['category_id'], 'merchant_id' => $orderFirst['merchant_num']));
            foreach ($accessoriesList as $k => $v) {
                foreach ($acc_cuz as $kc => $vc) {
                    if ($v['id'] == $vc['base_id']) {
                        $accessoriesList[$k]['name_ch'] = $vc['name_ch'];
                        $accessoriesList[$k]['name_en'] = $vc['name_en'];
                        break;

                    }
                }

            }

            //自定义随机配件 end


            foreach ($accessoriesList as $rs) {
                $productFirst['accessoriesStr'] .= $rs['name_' . $this->curLang] . ';';
            }
        }
        //#故障
        if ($productFirst['fault']) {
            $faultList = $this->common_model->selectCustom('mt_product_fault', '', 'id desc', array('id' => explode(',', $productFirst['fault'])));

            $fault_cuz = $this->common_model->selectCustom('mt_product_fault_customize', array('category' => $productFirst['category_id'], 'merchant_id' => $orderFirst['merchant_num']));
            foreach ($faultList as $k => $v) {
                foreach ($fault_cuz as $kc => $vc) {
                    if ($v['id'] == $vc['base_id']) {
                        $faultList[$k]['name_ch'] = $vc['name_ch'];
                        $faultList[$k]['name_en'] = $vc['name_en'];
                        break;

                    }
                }

            }

            foreach ($faultList as $rs) {
                $productFirst['faultStr'] .= $rs['name_' . $this->curLang] . ';';
            }
        }
        //#维修项
        if ($productFirst['repairItem']) {
            $ids = explode(',', $productFirst['repairItem']);
            if ($productFirst['supplementRepairItem']) {
                $supplementIds = explode(',', $productFirst['supplementRepairItem']);
                $ids = array_merge($ids, $supplementIds);
            }
            $repairItemList = $this->common_model->selectCustom('mt_product_repair_item', '', 'grade desc', array('id' => $ids));


            //自定义维修项begin 0401
            $models = $this->common_model->selectCustom('product_model', array('sap_id' => $productFirst['model_id']));
            $sql = sprintf("select * from mt_repair_item_customize where category_id = '%s' and customer_id = '%s' and `model_id`='%s' group by `labor_id`", $productFirst['category_id'], $orderFirst['merchant_num'], $models[0]['id']);
            $acc_item = $this->db->query($sql)->result_array();
            if ($acc_item) {
                foreach ($repairItemList as $k => $v) {
                    foreach ($acc_item as $kc => $vc) {
                        if ($v['id'] == $vc['labor_id']) {
                            $repairItemList[$k]['name_ch'] = $vc['name_ch'];
                            $repairItemList[$k]['name_en'] = $vc['name_en'];
                            break;
                        }
                    }
                }
            } else {
                $sql = sprintf("select * from mt_repair_item_customize where category_id = '%s' and customer_id = '%s' and `model_id` < 0 group by `labor_id`", $productFirst['category_id'], $orderFirst['merchant_num']);
                $acc_item = $this->db->query($sql)->result_array();
                foreach ($repairItemList as $k => $v) {
                    foreach ($acc_item as $kc => $vc) {
                        if ($v['id'] == $vc['labor_id']) {
                            $repairItemList[$k]['name_ch'] = $vc['name_ch'];
                            $repairItemList[$k]['name_en'] = $vc['name_en'];
                            break;
                        }
                    }
                }
            }
            //自定义维修项end

            foreach ($repairItemList as &$rs) {
                $productFirst['repairItemStr'] .= $rs['name_' . $this->curLang] . ';';
            }
        }
        //#维修轨迹
        $trackList = $this->common_model->selectCustom('mt_product_track', array('pid' => $id));
        foreach ($trackList as &$rs) {
            $status = $this->common_model->selectFirst('mt_product_status', array('id' => $rs['end_sid']));
            $rs['status_name'] = $status['name_' . $this->curLang];
            $rs['is_sync'] = $rs['is_sync'] ? 'Yes' : 'No';
            $rs['add_time'] = date('Y-m-d H:i:s', $rs['add_time']);
        }
        unset($rs);
        $data['trackList'] = $trackList;
        //#配件
        $partsList = $this->common_model->selectCustom('mt_product_parts', array('pid' => $id));
        foreach ($partsList as &$rs) {
            //#补充配件
            if ($rs['isSupplement']) {
                $rs['name'] = $rs['name'] . '&nbsp;<span style="color:red;">+</span>';
            }
            if ($rs['status'] == 2) {
                $rs['statusName'] = 'pending';
            } elseif ($rs['status'] == 3) {
                $rs['statusName'] = 'wait purchase';
            } else {
                $rs['statusName'] = 'Instock';
            }
            $rs['price'] = $rs['price'] . ' ' . $rs['currency'];
            $rs['rate_price'] = $rs['rate_price'] . ' ' . $rs['rate_currency'];
        }
        $data['partsList'] = $partsList;
        //#产品信息
        $data['productFirst'] = $productFirst;
        //hold记录
        $data['holdList'] = $this->common_model->selectCustom('mt_hold_log', array('pid' => $id));
        //日志
        $logList = $this->common_model->selectCustom('mt_product_log', array('pid' => $productFirst['id']));
        $aliasData = array('otherRemark' => 'otherRemark', 'status_id' => 'status_id', 'acceptQuotation' => 'acceptQuotation');
        foreach ($logList as &$rs) {
            $rs['add_time'] = date('Y-m-d H:i:s', $rs['add_time']);
            if ($rs['content']) {
                $contentArr = unserialize($rs['content']);
                foreach ($contentArr as $k => $val) {
                    if (!$aliasData[$k]) continue;
                    $rs['contentFormat'] .= '' . $aliasData[$k] . ' : ' . $val . ' <br />';
                }
            }
        }
        unset($rs);
        $data['logList'] = $logList;
        //#视图
        $data['curLang'] = $this->curLang;
        $this->common_model->flowBread($id, $productFirst['status_id'], $productFirst['oid']);
        $userRole = $this->users['role_id'];
        $roleArray=explode(',',$userRole);
        if ($isPay && (in_array(1,$roleArray) || in_array(5,$roleArray))) {
            $this->breadcrumb->append_crumb(
                array('url' => 'products/Product_new/repair/' . $id, 'name' => lang('product_bcbj'))
            );
        }
        if ($this->debug) {
            gsh_output($data);
        }
        $this->load->view('defaults/product_inDetail_new', $data);
        $this->load->view('defaults/common/footer');
    }


    /**
     * 修改产品
     * @param string $id
     */
    function edit($id = '')
    {
        //表单处理
        $data['post_type'] = __FUNCTION__;
        $role = $this->users['role'];
        $data['role'] = $role;
        $data['role_id'] = explode(',', $this->users['role_id']);
        //接收数据
        $id = intval($id) ? intval($id) : gp('id');
        $data['id'] = $id;
        $products = $this->common_model->selectFirst($this->t_product, array('id' => $id));
        $orderFirst = $this->common_model->selectFirst('mt_order', array('id' => $products['oid']));
        if (!$products) {
            show_error('this product not found');
        }
        //判断是否修改数据
        $formData = gp();
        if ($formData['submits']) {
            $inData = array();
            foreach ($formData as $key => $val) {
                if (array_key_exists($key, $products) && $products[$key] != $val) {
                    $inData[$key] = $val;
                }
            }
            if ($inData) {
                $inData['add_user'] = $this->users['name'];
                $inData['add_time'] = time();
                //#同意or拒绝 报价
                if ($inData['status_id'] == '6' || $inData['status_id'] == '7') {
                    $inData['acceptQuotation'] = $inData['status_id'] == '6' ? 1 : 0;
                    if ($inData['acceptQuotation'] & strtolower($orderFirst['point']) == 'es')
                    {
                        $partsList = $this->common_model->selectCustom('mt_product_parts', array('pid' => $id));
                        //WMS 锁定库存
                        $wms_lock_url = blang_url('wmsApi/Stock/lock');
                        $user = 'tomrepair.es';
                        $timestamp = time();
                        $pass = 'omstomrepaires20161214';
                        $sign = md5($user . $timestamp . $pass);
                        $data['user'] = $user;
                        $data['timestamp'] = $timestamp;
                        $data['sign'] = $sign;
                        $data['data']['parts_list'] = $partsList;
                        $data['data']['product_first'] = $products;
                        $response = CurlPost($wms_lock_url, $data);
                    }
                }
                /**
                 * 反悔拒绝报价取消采购
                 */
                if ($inData['status_id'] == '5' && $products['status_id'] == '6') {
                    if (ENVIRONMENT == 'development') {
                        $url = 'http://47.90.55.9:8889/webService/cancelAccessoryPurchaseNewOrder.php';
                    } else {
                        $url = 'http://stock.dhhtgroup.com/webService/cancelAccessoryPurchaseNewOrder.php';
                    }
                    $partsArr = $this->common_model->selectCustom('mt_product_parts', array('pid' => $id));
                    if (strtolower($orderFirst['point']) == 'es')
                    {
                        //WMS 释放库存
                        $wms_release_url = blang_url('wmsApi/Stock/release');
                        $user = 'tomrepair.es';
                        $timestamp = time();
                        $pass = 'omstomrepaires20161214';
                        $sign = md5($user . $timestamp . $pass);
                        $data['user'] = $user;
                        $data['timestamp'] = $timestamp;
                        $data['sign'] = $sign;
                        $data['data']['parts_list'] = $partsArr;
                        $response = CurlPost($wms_release_url, $data);
                    }
                    $partsIdArr = array();
                    foreach ($partsArr as $pa) {
                        $partsIdArr[] = $pa['id'];
                    }
                    $data = array(
                        'account' => 'tomrepair',
                        'token' => 'tomrepairAccessoryPurshase',
                        'data' => json_encode($partsIdArr)
                    );
                    //取消配件采购
                    $result = CurlPost($url, $data);
                    /*if($products['brandCustomerNum']==0) {
                        //补回资金记录
                        $deposit_record = $this->common_model->selectFirst('mt_deposit_record', array('orderId' => $products['order_num']));
                        if ($deposit_record) {
                            unset($deposit_record['id']);
                            $deposit_record['time'] = time();
                            $deposit_record['add_name'] = $this->users['name'];
                            $deposit_record['money'] = abs($deposit_record['money']);
                            $deposit_record['transactionId'] = '此订单之前同意报价时所收款项现因客户不同意报价终止维修而返还';
                            $this->common_model->insert('mt_deposit_record', $deposit_record);
                            $this->common_model->updateset('mt_customers', array('Balances' => 'Balances+' . $deposit_record['money']), array('id' => $deposit_record['customers_id']));
                        }
                    }*/
                }
                if($inData['status_id'] == '5'){
                    $inData['acceptQuotation'] = null;
                }
                //#更新产品
                $this->common_model->update($this->t_product, $inData, array('id' => $id));
                //#更新订单金额
                if (isset($inData['acceptQuotation'])) {
                    $this->common_model->setOrderPrice($products['oid']);
                }
                //#更新订单状态
                if ($inData['status_id']) {
                    $this->common_model->resetOrderStatus($products['oid']);
                }
                //#产品状态轨迹
                if ($inData['status_id']) {
                    $trackData = array(
                        'pid' => $id, 'oid' => $products['oid'], 'order_num' => $products['order_num'],
                        'productSku' => $products['productSku'], 'start_sid' => $products['status_id'],
                        'end_sid' => $inData['status_id'], 'uid' => $this->users['id'],
                        'add_user' => $this->users['name'], 'add_time' => $inData['add_time']
                    );
                    $this->common_model->insert('mt_product_track', $trackData);
                }
                //hold记录
                if($inData['available']==2){
                    $holdData=array('order_num'=>$orderFirst['order_num'],'pid'=>$products['id'],
                        'remark'=>'手动hold:'.$inData['otherRemark'],'name'=>$this->users['name'],'time'=>time(),
                        'type'=>1,'order_or_product'=>2,'off_or_on'=>1
                    );
                    $this->common_model->insert('mt_hold_log',$holdData);
                }
                //解除hold记录
                if($products['available']==2 && $inData['available']==1){
                    $holdData=array('order_num'=>$orderFirst['order_num'],'pid'=>$products['id'],
                        'remark'=>'手动解除hold','name'=>$this->users['name'],'time'=>time(),
                        'type'=>1,'order_or_product'=>2,'off_or_on'=>2
                    );
                    $this->common_model->insert('mt_hold_log',$holdData);
                }
                //日志
                $logData = array(
                    'pid' => $id, 'oid' => $products['oid'], 'order_num' => $products['order_num'],
                    'add_user' => $this->users['name'], 'add_time' => $inData['add_time'],
                    'uid' => $this->users['id'], 'content' => serialize($inData)
                );
                $this->common_model->insert('mt_product_log', $logData);
            }
            redirect(blang_url('products/Product_new/inDetails/' . $id));
        } else {
            $orders = $this->common_model->selectFirst('mt_order', array('id' => $products['oid']));
            if (!$orders) {
                show_error('This Order not found');
            }
            $serverType = $orders['service_type'];
            $serviceTypeFirst = $this->common_model->selectFirst('mt_service_type', array('id' => $serverType));
            $products['serviceTypeName'] = $serviceTypeFirst['name_' . $this->curLang];
            $data['orders'] = $orders;
            //获取状态
            $statusFirst = $this->common_model->selectFirst('mt_product_status', array('id' => $products['status_id']));
            $products['statusName'] = $statusFirst['name_' . $this->curLang];
            //下一步状态
            $nextStatusArr = explode(',', $statusFirst['next_status_' . $serverType]);
            $noNextStatusArr = array(2, 5, 17);
            $nextStatusArr = array_diff($nextStatusArr, $noNextStatusArr);
            $nextStatusArr[] = $products['status_id'];
            if ($products['status_id'] == 6 || $products['status_id'] == 7) {
                if ($products['brandCustomerNum'] == 0) {
                    $nextStatusArr[] = 5;
                }
            }
            $wherein = array('id' => $nextStatusArr);
            /*  注释可以改到同意OR拒绝报价
            if($products['status_id'] == 5){
                $wherein = array('id'=>array($products['status_id']));
            }
            */
            $statusList = $this->common_model->selectCustom('mt_product_status', '', 'id asc', $wherein);
            $data['statusList'] = $statusList;
            //default
            $ckdAvailable[$products['available']] = 'checked';
            $data['ckdAvailable'] = $ckdAvailable;
            $optStatus[$products['status_id']] = 'selected';
            $data['optStatus'] = $optStatus;
            //product
            $categoryFirst = $this->common_model->selectFirst('mt_product_category', array('id' => $products['category_id']));
            $products['category_name'] = $categoryFirst['name_' . $this->curLang];
            $data['productFirst'] = $products;
            //日志
            $logList = $this->common_model->selectCustom('mt_product_log', array('pid' => $products['id']));
            $aliasData = array('otherRemark' => 'otherRemark', 'status_id' => 'status_id', 'acceptQuotation' => 'acceptQuotation');
            foreach ($logList as &$rs) {
                $rs['add_time'] = date('Y-m-d H:i:s', $rs['add_time']);
                if ($rs['content']) {
                    $contentArr = unserialize($rs['content']);
                    foreach ($contentArr as $k => $val) {
                        if (!$aliasData[$k]) continue;
                        $rs['contentFormat'] .= '' . $aliasData[$k] . ' : ' . $val . ' <br />';
                    }
                }
            }
            unset($rs);
            $data['logList'] = $logList;
            //views
            $data['curLang'] = $this->curLang;
            if ($this->debug) {
                gsh_output($data);
            }
            $this->common_model->flowBread($id, $products['status_id'], $products['oid']);
            $this->load->view('defaults/product_edit_new', $data);
            $this->load->view('defaults/common/footer');
        }
    }


    /**
     * 拆包质检
     * @param string $id
     */
    function unpackQc($id = '')
    {
        //表单处理
        $data['post_type'] = __FUNCTION__;
        //接收数据
        $id = intval($id) ? intval($id) : gp('id');
        $data['id'] = $id;
        $productFirst = $this->common_model->selectFirst($this->t_product, array('id' => $id));
        if (isset($productFirst['inWarranty_tr'])) {
            if ($productFirst['inWarranty_tr'] == 1) {
                $productFirst['inWarranty_trName'] = 'This device has manufacturer warranty.';
            } else {
                $productFirst['inWarranty_trName'] = 'We do not find manufacturer warranty.';
            }
        }
        $status_id = $productFirst['status_id'];
        if (!$productFirst) {
            show_error('This Product not found');
        }
        $orderFirst = $this->common_model->selectFirst('mt_order', array('id' => $productFirst['oid']));
        //运费规则
        $isInFreight = false;
        if ($productFirst['status_id'] == 18 && !isset($orderFirst['shippingFeeText'])) {
            $pointRule = array(2, 5);
            if (in_array($orderFirst['service_center'], $pointRule)) {
                $isInFreight = true;
            } elseif ($orderFirst['service_center'] == 1 && $orderFirst['product_quantity'] > 1) {
                $isInFreight = true;
            }
        }
        $data['isInFreight'] = $isInFreight;
        if($productFirst['fault'] && $productFirst['status_id'] == 2){
            redirect(blang_url('products/Product_new/inDetails/' . $id));
            exit;
        }
        //判断是否修改数据
        $formData = gp();
        if ($formData['submits']) {

            //检测费开始 **********************************************************************
            //$orderFirst['service_type']=5 这个是检测，如果视图传值isUnpackPass为1，就是质检合格，质检合格只收一个质检费用
            if ($orderFirst['service_type'] == 5 && $formData['isUnpackPass'] == 1) {
                //获取了所有的价格
                $ckprice = $this->common_model->selectFirst('mt_base_price', array('labor_id' => 453, 'site_id' => $orderFirst['service_center'], 'category_id' => $productFirst['category_id']));
                //echo $this->db->last_query();
                $overhaulAmount = 0;
                $isSupplement = 0;
                //检测费用只向商家收费
                $overhaulAmount = 12.34;//$ckprice['merchant_price']; //检测费
                //产品费用修改
                $this->common_model->update('mt_product', array(
                    'merchantSubTotal' => $productFirst['merchantSubTotal'] + $overhaulAmount, //商家小记
                    'laborAmountMerchant' => $productFirst['laborAmountMerchant'] + $overhaulAmount, //商家手工费
                ), array('id' => $productFirst['id']));

                //订单费用修改
                $this->common_model->update('mt_order', array(
                    'orderSubtotalMerchant' => $orderFirst['orderSubtotalMerchant'] + $overhaulAmount, //订单商家小计总额
                    'grandTotalMerchant' => $orderFirst['grandTotalMerchant'] + $overhaulAmount, //支付商家总额
                ), array('id' => $orderFirst['id']));

                //添加扣款记录以及扣除款项
                $this->common_model->cutPayment($orderFirst['id'], $orderFirst['merchant_num'], $overhaulAmount, $isSupplement);
            }
            //检测费结束 **********************************************************************
            //修改订单费用
            if ($isInFreight) {
                $this->common_model->update('mt_order', array('shippingFeeText' => $formData['shippingFeeText']), array('id' => $orderFirst['id']));
            }
            $formData['accessories'] = implode(',', $formData['accessories']);
            $formData['fault'] = implode(',', $formData['fault']);
            $inData = array();
            foreach ($formData as $key => $val) {
                if (array_key_exists($key, $productFirst) && $productFirst[$key] != $val) {
                    $inData[$key] = $val;
                }
            }
            if ($inData) {
                //文件上传路径
                $uploaddir = './resource/uploadfiles/image/warrantyImg/';//上传的文件保存在当前目录
                //质保图片集
                $warranty_img = array();
                for ($i = 1; $i <= 3; $i++) {
                    if ($_FILES['warranty_img_' . $i]['tmp_name']) {
                        $result = uploadfile($_FILES['warranty_img_' . $i], $uploaddir, '', 19 * 1048576);
                        if ($result == 2) {
                            js_goto('', '文件大于19M！！！不能上传', 1);
                            exit();
                        } else {
                            $warranty_img[] = $result;
                        }
                    }
                }
                if ($warranty_img) {
                    $inData['warranty_img_qc'] = implode(',', $warranty_img);
                }

                /*
                //TR保内，OMS保外
                if($orderFirst['service_type']!=5 && $productFirst['seller_warranty']=='1' && $inData['inWarranty_qc']=='0'){
                    $inData['available'] = 2;
                }
                */
                //发送HOLD邮件
                if ($inData['available'] == 2) {
                    $customerNum = $orderFirst['merchant_num'] ? $orderFirst['merchant_num'] : $orderFirst['consumer_num'];
                    $orderCustomer = $this->common_model->selectFirst('mt_customers', array('id' => $customerNum));
                    sendEmail(array('assign' => 'email_inWarranty', 'emails' => $orderCustomer['email'], 'code' => $orderFirst['order_num']));
                }
                //发送IMEI邮件
                if(substr($orderFirst['source_oid'],0,2) != 'OM'){
                    $email = $orderFirst['consumer_email'];
                    if(in_array($orderFirst['point'],array('es','de','ru','sz'))){
                        $foundAssign = 'email_foundImei_'.$orderFirst['point'];
                        $updateAssign = 'email_updateImei_'.$orderFirst['point'];
                    }else{
                        $foundAssign = 'email_foundImei_en';
                        $updateAssign = 'email_updateImei_en';
                    }
                    if($inData['cb_qc_imei']){
                        if(!$productFirst['imei']){
                            sendEmail(array(
                                'assign'=>$foundAssign,'emails'=>$email,
                                'code'=>$inData['cb_qc_imei']
                            ));
                        }elseif($productFirst['imei'] != $inData['cb_qc_imei']){
                            sendEmail(array(
                                'assign'=>$updateAssign,'emails'=>$email,
                                'code'=>$inData['cb_qc_imei']
                            ));
                        }
                    }
                }
                //修改产品
                $this->common_model->update($this->t_product, $inData, array('id' => $id));
                //#更新状态
                if ($status_id == 18 || $status_id == 15) {
                    $this->common_model->resetStatus($id);
                }
            }
            //调试信息
            if (!$this->debug) {
                redirect(blang_url('products/Product_new/inDetails/' . $id));
            }
        } else {
            $serviceType = $this->common_model->selectFirst('mt_service_type', array('id' => $orderFirst['service_type']));
            $productFirst['serviceTypeName'] = $serviceType['name_' . $this->curLang];
            $statusFirst = $this->common_model->selectFirst('mt_product_status', array('id' => $status_id));
            $productFirst['statusName'] = $statusFirst['name_' . $this->curLang];
            if ($productFirst['brandCustomerNum']) {
                $brandCustomer = $this->common_model->selectFirst('mt_customers', array('id' => $productFirst['brandCustomerNum']));
                $data['brandCustomer'] = $brandCustomer;
            }
            $categoryFirst = $this->common_model->selectFirst('mt_product_category', array('id' => $productFirst['category_id']));
            $productFirst['category_name'] = $categoryFirst['name_' . $this->curLang];
            //调用订单所属商家信息 sqingwen 20170222 begin
            $customerNum = $orderFirst['merchant_num'] ? $orderFirst['merchant_num'] : $orderFirst['consumer_num'];
            $orderCustomer = $this->common_model->selectFirst('mt_customers', array('id' => $customerNum));
            $data['merchant'] = $orderCustomer;
            $this->load->library('barcode');
            $type = "C128";//$gp['tc'];
            $w = '2';
            $h = '15';
            $code = '<img src="data:image/png;base64,' . $this->barcode->getBarcodePNG($id, $type, $w, $h) . '"/>';
            $data['tt_barcode'] = $code;
            //调用订单所属商家信息 sqingwen 20170222 end
            //生成条码
            if (!$productFirst['barcode']) {
                $this->load->library('barcode');
                $barcodeText = sprintf("% 10s", $productFirst['id']);
                $htmlBarcode = $this->barcode->getBarcodeHTML($barcodeText, "C128");
                $productFirst['barcode'] = $htmlBarcode;
                $this->common_model->update('mt_product', array('barcode' => $htmlBarcode), array('id' => $productFirst['id']));
            }
            //成色
            $damageList = $this->common_model->selectCustom('mt_product_damage');
            $data['damageList'] = $damageList;
            //随机附件
            $accessoriesList = $this->common_model->selectCustom('mt_product_accessory', array('category' => $productFirst['category_id']));
            //自定义的随机附件
            $acc_cuz = $this->common_model->selectCustom('mt_product_accessory_customize', array('category' => $productFirst['category_id'], 'merchant_id' => $orderFirst['merchant_num']));
            foreach ($accessoriesList as $k => $v) {
                foreach ($acc_cuz as $kc => $vc) {
                    if ($v['id'] == $vc['base_id']) {
                        $accessoriesList[$k]['name_ch'] = $vc['name_ch'];
                        $accessoriesList[$k]['name_en'] = $vc['name_en'];
                        break;
                    }
                }

            }
            $data['accessoriesList'] = $accessoriesList;
            //故障
            $faultList = $this->common_model->selectCustom('mt_product_fault', array('category' => $productFirst['category_id']));
            //自定义的随机附件
            $fault_cuz = $this->common_model->selectCustom('mt_product_fault_customize', array('category' => $productFirst['category_id'], 'merchant_id' => $orderFirst['merchant_num']));
            foreach ($faultList as $k => $v) {
                foreach ($fault_cuz as $kc => $vc) {
                    if ($v['id'] == $vc['base_id']) {
                        $faultList[$k]['name_ch'] = $vc['name_ch'];
                        $faultList[$k]['name_en'] = $vc['name_en'];
                        break;

                    }
                }

            }

            $data['faultList'] = $faultList;
            //default
            $ckdAvailable[$productFirst['available']] = 'checked';
            $data['ckdAvailable'] = $ckdAvailable;
            $ckdIsUnpackPass[$productFirst['isUnpackPass']] = 'checked';
            $data['ckdIsUnpackPass'] = $ckdIsUnpackPass;
            $ckdDesCst[$productFirst['descriptionConsistent']] = 'checked';
            $data['ckdDesCst'] = $ckdDesCst;
            $ckdDamage[$productFirst['physicalAppearance']] = 'checked';
            $data['ckdDamage'] = $ckdDamage;

            //型号判断是否保内
            if ($productFirst['model_id']) {
                $siteModelAry = array(1 => '1', 2 => '4', 3 => '3', 4 => '2', 5 => '5', 6 => '');
                $siteVal = $siteModelAry[$orderFirst['service_center']];
                $models = $this->common_model->selectFirst('site_product_model', array('bid'=>$brandCustomer['brand_id'],'mid' => $productFirst['model_id'], 'warranty_type' => 0, 'site' => $siteVal));
                if ($models) {
                    $productFirst['inWarranty_qc'] = '0';
                }
            }
            $ckdInWarranty[$productFirst['inWarranty_qc']] = 'checked';
            $data['ckdInWarranty'] = $ckdInWarranty;
            //附件
            $product_Accessories = explode(',', $productFirst['accessories']);
            foreach ($product_Accessories as $val) {
                $ckdAccessories[$val] = 'checked';
                $data['ckdAccessories'] = $ckdAccessories;
            }
            //故障
            $product_Fault = explode(',', $productFirst['fault']);
            foreach ($product_Fault as $val) {
                $ckdFault[$val] = 'checked';
                $data['ckdFault'] = $ckdFault;
            }

            $data['productFirst'] = $productFirst;
            $data['orderFirst'] = $orderFirst;
            //调试信息
            if ($this->debug) {
                gsh_output($data);
            }
            //views
            $data['curLang'] = $this->curLang;
            $this->common_model->flowBread($id, $productFirst['status_id'], $productFirst['oid']);
            $this->load->view('defaults/product_unpackQc_new', $data);
            $this->load->view('defaults/common/footer');
        }
    }


    /**
     * 接单
     * @param string $id
     */
    function joinProduct($id = '')
    {
        $productFirst = $this->common_model->selectFirst($this->t_product, array('id' => $id));
        $status_id = $productFirst['status_id'];
        //#更新状态
        if ($status_id == 2) {
            $this->common_model->resetStatus($id);
        }
        //调试信息
        if (!$this->debug) {
            redirect(blang_url('products/Product_new/repair/' . $id));
        }
    }


    /**
     * 维修报价
     * @param string $id
     */
    function repair($id = '')
    {
        //表单处理
        $data['post_type'] = __FUNCTION__;
        //接收数据
        $id = intval($id) ? intval($id) : gp('id');
        $data['id'] = $id;
        $productFirst = $this->common_model->selectFirst($this->t_product, array('id' => $id));
        $status_id = $productFirst['status_id'];
        if (!$productFirst) {
            show_error('This Product not found');
        }
        $orderFirst = $this->common_model->selectFirst('mt_order', array('id' => $productFirst['oid']));
        $isPay = $orderFirst['is_pay'];
        if (!$orderFirst) {
            $this->common_model->delete($this->t_product, array('oid' => $productFirst['oid']));
            show_error('此维修单不存在,将删除此产品数据');
        }
        //判断是否修改数据
        $formData = gp();
        if ($formData['submits']) {
            $repairItem = implode(',', $formData['repairItem']);
            if($repairItem==''){
                js_goto('','维修项不能为空');
            }
            //补充报价
            if ($isPay) {
                $formData['supplementRepairItem'] = $repairItem;
                unset($formData['repairItem']);
            } else {
                $formData['repairItem'] = $repairItem;
            }
            $inData = array();
            foreach ($formData as $key => $val) {
                if (array_key_exists($key, $productFirst) && $productFirst[$key] != $val) {
                    if ($key == 'technicianRemark' || $key == 'warranty_remark_rp') {
                        //$inData[$key] = serialize(array('v' => $val, 't' => time()));//新增添加时间
                        $inData[$key] = $val . PHP_EOL . "[".date("Y-m-d H:i:s", time()).']';
                    } else {
                        $inData[$key] = $val;
                    }

                }
            }

            if ($inData) {

                //文件上传路径
                $uploaddir = './resource/uploadfiles/image/warrantyImg/';//上传的文件保存在当前目录
                //质保图片集
                $warranty_img = array();

                for ($i = 1; $i <= 3; $i++) {

                    if ($_FILES['warranty_img_' . $i]['tmp_name']) {

                        $result = uploadfile($_FILES['warranty_img_' . $i], $uploaddir, '', 19 * 1048576);

                        if ($result == 2) {
                            js_goto('', '文件大于19M！！！不能上传', 1);
                            exit();
                        } else {
                            $warranty_img[] = $result;
                        }
                    }
                }
                if ($warranty_img) {
                    $inData['warranty_img_rp'] = implode(',', $warranty_img);
                }

                //TR保内，OMS保外
                if(!$productFirst['brandCustomerNum'] || $productFirst['brandCustomerNum']=='0'){
                    if($orderFirst['service_type']!=5 && $productFirst['seller_warranty']==='1' && $inData['inWarranty']==='0'){
                        $inData['available'] = 2;
                    }
                }
                $this->common_model->update($this->t_product, $inData, array('id' => $id));
                //需求变动--需要确认配件信息之后才更新状态和报价
                /*$this->feeModel->setProductPrice($id);
                //#更新状态
                if ($status_id == 3) {
                    $this->common_model->resetStatus($id);
                }*/
                //发送邮件
                if ($inData['available'] == 2) {
                    $holdData=array('order_num'=>$orderFirst['order_num'],'pid'=>$productFirst['id'],
                        'remark'=>'质保类型不一致导致hold','name'=>$this->users['name'],'time'=>time(),
                        'type'=>2,'order_or_product'=>2,'off_or_on'=>1
                    );
                    $this->common_model->insert('mt_hold_log',$holdData);
                    $customerNum = $orderFirst['merchant_num'] ? $orderFirst['merchant_num'] : $orderFirst['consumer_num'];
                    $orderCustomer = $this->common_model->selectFirst('mt_customers', array('id' => $customerNum));
                    sendEmail(array('assign' => 'email_inWarranty', 'emails' => $orderCustomer['email'], 'code' => $orderFirst['order_num']));
                }

            }
            //调试信息
            if (!$this->debug) {
                redirect(blang_url('products/Product_new/repair/' . $id));
            }
        } else {
            $serviceType = $this->common_model->selectFirst('mt_service_type', array('id' => $orderFirst['service_type']));
            $productFirst['serviceTypeName'] = $serviceType['name_' . $this->curLang];
            $statusFirst = $this->common_model->selectFirst('mt_product_status', array('id' => $status_id));
            $productFirst['statusName'] = $statusFirst['name_' . $this->curLang];
            $brandCustomer = $this->common_model->selectFirst('mt_customers', array('id' => $productFirst['brandCustomerNum']));
            $data['brandCustomer'] = $brandCustomer;
            $categoryFirst = $this->common_model->selectFirst('mt_product_category', array('id' => $productFirst['category_id']));
            $productFirst['category_name'] = $categoryFirst['name_' . $this->curLang];
            $data['productFirst'] = $productFirst;
            $data['orderFirst'] = $orderFirst;
            //维修项
            $repairItemList = $this->common_model->selectCustom('mt_product_repair_item', array('category' => $productFirst['category_id']));
             //自定义维修项begin 0401
            $models = $this->common_model->selectCustom('product_model', array('sap_id' => $productFirst['model_id']));
            $sql = sprintf("select * from mt_repair_item_customize where category_id = '%s' and customer_id = '%s' and `model_id`='%s' group by `labor_id`", $productFirst['category_id'], $orderFirst['merchant_num'], $models[0]['id']);
            $acc_item = $this->db->query($sql)->result_array();
            if ($acc_item) {
                foreach ($repairItemList as $k => $v) {
                    foreach ($acc_item as $kc => $vc) {
                        if ($v['id'] == $vc['labor_id']) {
                            $repairItemList[$k]['name_ch'] = $vc['name_ch'];
                            $repairItemList[$k]['name_en'] = $vc['name_en'];
                            break;
                        }
                    }
                }
            } else {
                $sql = sprintf("select * from mt_repair_item_customize where category_id = '%s' and customer_id = '%s' and `model_id` < 0 group by `labor_id`", $productFirst['category_id'], $orderFirst['merchant_num']);
                $acc_item = $this->db->query($sql)->result_array();
                foreach ($repairItemList as $k => $v) {
                    foreach ($acc_item as $kc => $vc) {
                        if ($v['id'] == $vc['labor_id']) {
                            $repairItemList[$k]['name_ch'] = $vc['name_ch'];
                            $repairItemList[$k]['name_en'] = $vc['name_en'];
                            break;
                        }
                    }
                }
            }
            //自定义维修项end

            $data['repairItemList'] = $repairItemList;
            //default
            $ckdAvailable[$productFirst['available']] = 'checked';
            $data['ckdAvailable'] = $ckdAvailable;
            $ckdInWarranty[$productFirst['inWarranty']] = 'checked';
            $data['ckdInWarranty'] = $ckdInWarranty;
            //#维修项
            $productFirst['repairItem'] = $isPay ? $productFirst['supplementRepairItem'] : $productFirst['repairItem'];
            if ($productFirst['repairItem']) {
                $product_repairItem = explode(',', $productFirst['repairItem']);
                foreach ($product_repairItem as $val) {
                    $ckdRepairItem[$val] = 'checked';
                    $data['ckdRepairItem'] = $ckdRepairItem;
                }
            }
            //配件部位
            $productType = array('1' => 195);
            if (in_array($productFirst['category_id'], array_keys($productType))) {
                $partsPosition = $this->common_model->getQuery("SELECT a.* from product_model a LEFT JOIN product_association_b b on a.sap_id=b.mid where b.tid=15 and b.bid=" . $productType[$productFirst['category_id']], 'result_array');
                //重构数组
                $lang=$this->curLang;
                foreach($partsPosition as &$row){
                    if($lang!='ch'){
                        $row['name']=$row['name_en'];
                    }
                }
                unset($row);
                $data['partsPosition']=$partsPosition;
            } else {
                $data['partsPosition'] = array();
            }
            //加载获取配件列表service
            $this->load->service('wms/api_service');
            $result=$this->api_service->get_parts($orderFirst['point'],$orderFirst['merchant_num'],$productFirst['inWarranty'],1,'','',$productFirst['productSku'],'','','',$productFirst['brandCustomerNum']);
//            echo '<pre>';
//            print_r($result);
            $partsList=array();
            $lang = $this->uri->segment(1);
            if (!$result['success']) {
                $partsList = array();
            }else {
                $partsList = $result['data'];
            }
            foreach ($partsList as &$rs) {
                if ($lang == 'sc' && $rs['products_name_cn']) {
                    $rs['name'] = $rs['products_name_cn'];
                } else {
                    $rs['name'] = $rs['products_name'] ? $rs['products_name'] : $rs['products_name_cn'];
                }
                $rs['quantity'] = $rs['inventory'][0]['quantity'];
                $rs['price'] = $rs['products_price'];
                $rs['cost_price'] = $rs['products_cost'];
                $rs['point'] = $orderFirst['point'];
                $rs['whs_code'] = $rs['inventory'][0]['whs_code'];
                $rs['type'] = $rs['inventory'][0]['type'];
                $rs['brandCustomerNum'] = $productFirst['brandCustomerNum'];
                $rs['model_id'] = $productFirst['model_id'];
                $rs['jsonData'] = urlencode(json_encode($rs));
            }
            unset($rs);
            //var_dump($partsList);
            $data['partsList'] = $partsList;
            //views
            $data['curLang'] = $this->curLang;
            $this->common_model->flowBread($id, $productFirst['status_id'], $productFirst['oid']);
            $this->load->view('defaults/product_repair_new', $data);
            $this->load->view('defaults/common/footer');
        }
    }


    /**
     * 开始维修
     * @param string $id
     */
    function startRepair($id = '')
    {
        $productFirst = $this->common_model->selectFirst($this->t_product, array('id' => $id));
        $orderFirst = $this->common_model->selectFirst('mt_order', array('id' => $productFirst['oid']));
        $partsInfo = $this->common_model->selectCustom('mt_product_parts', array('pid' => $id));
        $status_id = $productFirst['status_id'];

        //#更新状态
        if ($status_id == 6) {
            $success = $this->common_model->resetStatus($id);
            if ($success && count($partsInfo) > 0 && $orderFirst['napa_stores_num']) {
                //开始维修同时WMS出库配件
                //加载出库service
                $this->load->service('/wms/inventory_service');
                $isout = $this->inventory_service->out_stock_mt($partsInfo, $orderFirst['napa_stores_num']);
                if (true !== $isout) {
                    //插入除错列表
                }
            }
        }
        //调试信息
        if (!$this->debug) {
            redirect(blang_url('products/Product_new/inDetails/' . $id));
        }
    }


    /**
     * 装配完成待QC
     * @param string $id
     */
    function assembledQc($id = '')
    {
        $productFirst = $this->common_model->selectFirst($this->t_product, array('id' => $id));
        $status_id = $productFirst['status_id'];
        //#更新状态
        if ($status_id == 7) {
            $this->common_model->resetStatus($id);
        }
        //调试信息
        if (!$this->debug) {
            redirect(blang_url('products/Product_new/inDetails/' . $id));
        }
    }


    /**
     * 维修完成
     * @param string $id
     */
    function endRepair($id = '')
    {
        $productFirst = $this->common_model->selectFirst($this->t_product, array('id' => $id));
        $status_id = $productFirst['status_id'];
        //#更新状态
        if ($status_id == 9) {
            $this->common_model->resetStatus($id);
        }
        //调试信息
        if (!$this->debug) {
            redirect(blang_url('products/Product_new/inDetails/' . $id));
        }
    }


    /**
     * 入库完成
     * @param string $id
     */
    function inStock($id = '')
    {
        $productFirst = $this->common_model->selectFirst($this->t_product, array('id' => $id));
        $status_id = $productFirst['status_id'];
        //#更新状态
        if ($status_id == 12) {
            $this->common_model->resetStatus($id);
        }
        //调试信息
        if (!$this->debug) {
            redirect(blang_url('products/Product_new/inDetails/' . $id));
        }
    }


    /**
     * 接单质检
     * @param string $id
     */
    function joinQc($id = '')
    {
        $productFirst = $this->common_model->selectFirst($this->t_product, array('id' => $id));
        $status_id = $productFirst['status_id'];
        //#更新状态
        $qcStatusArr = array(10, 15, 16);
        if (in_array($status_id, $qcStatusArr)) {
            $this->common_model->resetStatus($id);
        }
        //调试信息
        if (!$this->debug) {
            redirect(blang_url('products/Product_new/inDetails/' . $id));
        }
    }


    /**
     * 质检
     * @param string $id
     */
    function serviceQc($id = '')
    {
        //表单处理
        $data['post_type'] = __FUNCTION__;
        //接收数据
        $id = intval($id) ? intval($id) : gp('id');
        $data['id'] = $id;
        $productFirst = $this->common_model->selectFirst($this->t_product, array('id' => $id));
        $status_id = $productFirst['status_id'];
        if (!$productFirst) {
            show_error('This Product not found');
        }
        $orderFirst = $this->common_model->selectFirst('mt_order', array('id' => $productFirst['oid']));
        if (!$orderFirst) {
            $this->common_model->delete($this->t_product, array('oid' => $productFirst['oid']));
            show_error('此维修单不存在,将删除此产品数据');
        }
        //判断是否修改数据
        $formData = gp();
        if ($formData['submits']) {
            $formData['accessories'] = implode(',', $formData['accessories']);
            $formData['fault'] = implode(',', $formData['fault']);
            $inData = array();
            foreach ($formData as $key => $val) {
                if (array_key_exists($key, $productFirst) && $productFirst[$key] != $val) {
                    $inData[$key] = $val;
                }
            }
            if ($inData) {
                //发送邮件
                if ($productFirst['brandCustomerNum']) {
                    $customerNum = $productFirst['brandCustomerNum'];
                } else {
                    $customerNum = $orderFirst['merchant_num'] ? $orderFirst['merchant_num'] : $orderFirst['consumer_num'];
                }
                $orderCustomer = $this->common_model->selectFirst('mt_customers', array('id' => $customerNum));
                //sendEmail(array('assign' => 'email_inWarranty', 'emails' => $orderCustomer['email'], 'code' => $orderFirst['order_num']));
                //发送IMEI邮件
                if(substr($orderFirst['source_oid'],0,2) != 'OM'){
                    $email = $orderFirst['consumer_email'];
                    if(in_array($orderFirst['point'],array('es','de','ru','sz'))){
                        $foundAssign = 'email_foundImei_'.$orderFirst['point'];
                        $updateAssign = 'email_updateImei_'.$orderFirst['point'];
                    }else{
                        $foundAssign = 'email_foundImei_en';
                        $updateAssign = 'email_updateImei_en';
                    }
                    if($inData['ck_qc_imei']){
                        if(!$productFirst['imei']){
                            sendEmail(array(
                                'assign'=>$foundAssign,'emails'=>$email,
                                'code'=>$inData['ck_qc_imei']
                            ));
                        }elseif($productFirst['imei'] != $inData['ck_qc_imei']){
                            sendEmail(array(
                                'assign'=>$updateAssign,'emails'=>$email,
                                'code'=>$inData['ck_qc_imei']
                            ));
                        }
                    }
                }
                //修改产品信息
                $this->common_model->update($this->t_product, $inData, array('id' => $id));
            }

            //#更新状态
            if ($status_id == 11) {
                $this->common_model->resetStatus($id);
            }
            //调试信息
            if (!$this->debug) {
                redirect(blang_url('products/Product_new/inDetails/' . $id));
            }
        } else {
            $serviceType = $this->common_model->selectFirst('mt_service_type', array('id' => $orderFirst['service_type']));
            $productFirst['serviceTypeName'] = $serviceType['name_' . $this->curLang];
            $statusFirst = $this->common_model->selectFirst('mt_product_status', array('id' => $status_id));
            $productFirst['statusName'] = $statusFirst['name_' . $this->curLang];
            if ($productFirst['brandCustomerNum']) {
                $brandCustomer = $this->common_model->selectFirst('mt_customers', array('id' => $productFirst['brandCustomerNum']));
                $data['brandCustomer'] = $brandCustomer;
            }
            $categoryFirst = $this->common_model->selectFirst('mt_product_category', array('id' => $productFirst['category_id']));
            $productFirst['category_name'] = $categoryFirst['name_' . $this->curLang];
            $data['productFirst'] = $productFirst;
            $data['orderFirst'] = $orderFirst;
            //成色
            $damageList = $this->common_model->selectCustom('mt_product_damage');
            $data['damageList'] = $damageList;
            //随机附件
            $accessoriesList = $this->common_model->selectCustom('mt_product_accessory', array('category' => $productFirst['category_id']));
            //自定义随机配件 begin
            $acc_cuz = $this->common_model->selectCustom('mt_product_accessory_customize', array('category' => $productFirst['category_id'], 'merchant_id' => $orderFirst['merchant_num']));
            foreach ($accessoriesList as $k => $v) {
                foreach ($acc_cuz as $kc => $vc) {
                    if ($v['id'] == $vc['base_id']) {
                        $accessoriesList[$k]['name_ch'] = $vc['name_ch'];
                        $accessoriesList[$k]['name_en'] = $vc['name_en'];
                        break;
                    }
                }
            }
            $data['accessoriesList'] = $accessoriesList;
            //故障
            $faultList = $this->common_model->selectCustom('mt_product_fault', array('category' => $productFirst['category_id']));
            $fault_cuz = $this->common_model->selectCustom('mt_product_fault_customize', array('category' => $productFirst['category_id'], 'merchant_id' => $orderFirst['merchant_num']));
            foreach ($faultList as $k => $v) {
                foreach ($fault_cuz as $kc => $vc) {
                    if ($v['id'] == $vc['base_id']) {
                        $faultList[$k]['name_ch'] = $vc['name_ch'];
                        $faultList[$k]['name_en'] = $vc['name_en'];
                        break;
                    }
                }
            }
            $data['faultList'] = $faultList;
            //default
            $ckdAvailable[$productFirst['available']] = 'checked';
            $data['ckdAvailable'] = $ckdAvailable;
            $ckdIsQcPass[$productFirst['isQcPass']] = 'checked';
            $data['ckdIsQcPass'] = $ckdIsQcPass;
            $ckdDesCst[$productFirst['descriptionConsistent']] = 'checked';
            $data['ckdDesCst'] = $ckdDesCst;
            $ckdDamage[$productFirst['physicalAppearance']] = 'checked';
            $data['ckdDamage'] = $ckdDamage;
            $ckdInWarranty[$productFirst['inWarranty']] = 'checked';
            $data['ckdInWarranty'] = $ckdInWarranty;
            //附件
            $product_Accessories = explode(',', $productFirst['accessories']);
            foreach ($product_Accessories as $val) {
                $ckdAccessories[$val] = 'checked';
                $data['ckdAccessories'] = $ckdAccessories;
            }
            //故障
            $product_Fault = explode(',', $productFirst['fault']);
            foreach ($product_Fault as $val) {
                $ckdFault[$val] = 'checked';
                $data['ckdFault'] = $ckdFault;
            }
            //调试信息
            if ($this->debug) {
                gsh_output($data);
            }
            //views
            $data['curLang'] = $this->curLang;
            $this->common_model->flowBread($id, $productFirst['status_id'], $productFirst['oid']);
            $this->load->view('defaults/product_serviceQc_new', $data);
            $this->load->view('defaults/common/footer');
        }
    }


    /**
     * 已发货
     * @param string $id
     */
    function shipped($id = '')
    {
        $productFirst = $this->common_model->selectFirst($this->t_product, array('id' => $id));
        $status_id = $productFirst['status_id'];
        //#更新状态
        if ($status_id == 14 || $status_id == 18) {
            $this->common_model->resetStatus($id);
        }
        //调试信息
        if (!$this->debug) {
            redirect(blang_url('products/Product_new/inDetails/' . $id));
        }
    }


    /**
     * 已取件
     * @param string $id
     */
    function picked($id = ''){
        $productFirst = $this->common_model->selectFirst($this->t_product, array('id' => $id));
        $status_id = $productFirst['status_id'];
        //#更新状态
        if ($status_id == 19) {
            $this->common_model->resetStatus($id);
        }
        //调试信息
        if(!$this->debug){
            redirect(blang_url('products/Product_new/inDetails/' . $id));
        }
    }




    /**
     * 删除
     * @param string $id
     * @param string $page
     */
    function del($id = '', $page = '')
    {
        $id = intval($id);
        $where = array('id' => $id);
        $this->common_model->delete($this->t_product, $where);
        redirect(blang_url('product/Product_new/index/?page=' . intval($page)));
    }


    /**
     * 批量删除产品
     */
    function delBatch()
    {
        $oids = array('2119', '2122', '2127', '2129', '2140', '2142', '2143', '2234', '2415', '2416', '2417', '2418', '2419', '2420', '2471', '2472', '2475', '2479', '2518', '2520', '2521', '2523', '2528', '2593', '2595', '2597', '2599', '2602', '2604', '2651', '2670', '2673', '2675', '2676', '2677', '2679', '2681', '2682', '2684');
        foreach ($oids as $oid) {
            $this->output->enable_profiler(TRUE);
            $products = $this->common_model->selectFirst('mt_product', array('oid' => $oid), 'status_id desc');
            $pid = $products['id'];
            if (!$oid || !$pid) continue;
            $sql = "delete from mt_product where oid = $oid and id<>$pid";
            $this->db->query($sql);
        }
    }


    /**
     * 刷新产品价格
     * @param string $id
     * @return bool
     */
    function reloadPrice($id = '')
    {
        $id = intval($id);
        $products = $this->common_model->selectFirst('mt_product', array('id' => $id));
        if ($products['status_id'] > 5) {
            //show_error('维修意见已确认，无法再更新价格');
        }
        $this->common_model->clearProduct($id);
        $this->feeModel->setProductPrice($id);
        if (!$this->debug) {
            redirect(blang_url('products/Product_new/inDetails/' . $id));
        }
        return true;
    }

    /**
     * 产品报价
     * @param string $id
     */
    function  ProductQuote(){
        $gp=gp();
        $id=$gp['id'];
        if($id) {
            $productFirst = $this->common_model->selectFirst($this->t_product, array('id' => $id));
            $status_id = $productFirst['status_id'];
	        $orderFirst = $this->common_model->selectFirst('mt_order', array('id' => $productFirst['oid']));

            //为递四方保存当时的汇率 begin
            if ($orderFirst['merchant_num'] == 1028) {
                $rtData['rate'] = currency_service($orderFirst['currency_code'], 'CNY', 1);            
	     
                $rtData['order_num'] = $productFirst['order_num'];
                $rtData['currency'] = $orderFirst['currency_code'];
                $rtData['add_time'] = time();
                $this->common_model->insert('mt_product_rate', $rtData);
            }
            //为递四方保存当时汇率 end
            $isPay = $orderFirst['is_pay'];
	        $productFirst['repairItem'] = $isPay ? $productFirst['supplementRepairItem'] : $productFirst['repairItem'];
            //#更新状态
            if ($productFirst['repairItem']) {
                //刷新报价
                $this->feeModel->setProductPrice($id);
                $this->common_model->resetStatus($id);
                $productFirstNew = $this->common_model->selectFirst($this->t_product, array('id' => $id));
                if($productFirstNew['status_id']==6){
                    json_output(array('hint'=>'客户自动同意报价','url'=>blang_url('products/Product_new/startRepair/' . $id)),'维修报价完成',true);
                }else{
                    json_output(array('hint'=>'等待客户同意报价','url'=>blang_url('products/Product_new/inDetails/' . $id)),'维修报价完成',true);
                }
            } else {
                json_output(array(),'维修项不能为空',false);
            }
        }else{
            json_output(array(),'数据异常',false);
        }
    }


}
